﻿using Newtonsoft.Json;
using Omabar.BLL;
using Omabar.Common;
using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Omabar.CMS.Handler
{
    /// <summary>
    /// TripAdd 的摘要说明
    /// </summary>
    public class TripAdd : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            RESP_Info info = new RESP_Info();
            try
            {
                context.Response.ContentType = "text/plain";
                string TRIP_NAME = context.Request["TRIP_NAME"];
                string TRIP_DES = context.Request["TRIP_DES"];
                string[] flights = string.IsNullOrEmpty(context.Request["flight[0][]"]) ? new string[] { } : context.Request["flight[0][]"].Split(',');
                string[] photos = string.IsNullOrEmpty(context.Request["photo[0][]"]) ? new string[] { } : context.Request["photo[0][]"].Split(',');
                string[] hotels = string.IsNullOrEmpty(context.Request["hotel[0][]"]) ? new string[] { } : context.Request["hotel[0][]"].Split(',');

                string[] foods = string.IsNullOrEmpty(context.Request["food[0][]"]) ? new string[] { } : context.Request["food[0][]"].Split(',');

                Trip_BLL tripBLL = new Trip_BLL();
                TRIP trip = new TRIP()
                {
                    TRIP_ID = Util.GetUID(),
                    TRIP_NAME = TRIP_NAME,
                    TRIP_DES = TRIP_DES,
                    TRIP_FOOD = foods.Length == 0 ? "0" : foods[0]
                };

                info = tripBLL.Add(trip);

                //航班
                Trip_Flight_BLL tripFlightBLL = new Trip_Flight_BLL();

                foreach (string flight in flights)
                {
                    info = tripFlightBLL.Add(new TRIP_FLIGHT()
                    {
                        TRIP_ID = trip.TRIP_ID,
                        FLIGHT_ID = flight
                    });

                    System.Threading.Thread.Sleep(100);
                }

                //图库
                Trip_Gallery_BLL tripGalleryBLL = new Trip_Gallery_BLL();
                foreach (string photo in photos)
                {
                    info = tripGalleryBLL.Add(new TRIP_GALLERY()
                    {
                        TRIP_ID = trip.TRIP_ID,
                        PHOTO_ID = photo
                    });

                    System.Threading.Thread.Sleep(100);
                }

                //酒店
                Trip_Hotel_BLL tripHotelBLL = new Trip_Hotel_BLL();
                foreach (string hotel in hotels)
                {
                    info = tripHotelBLL.Add(new TRIP_HOTEL()
                    {
                        TRIP_ID = trip.TRIP_ID,
                        HOTEL_ID = hotel
                    });
                }

                //餐饮
                //Trip_Food_BLL tripFoodBLL = new Trip_Food_BLL();
                //foreach (string food in foods)
                //{
                //    info = tripFoodBLL.Add(new TRIP_FOOD()
                //    {
                //        TRIP_ID = trip.TRIP_ID,
                //        FOOD_ID = food
                //    });
                //}
                info.Tip = "编辑成功";
                info.Url = "TripList.aspx";
            }
            catch (Exception ex)
            {
                Tom.Net.Log.LogManager.Error("添加行程失败", ex);
                info.Success = false;
                info.Title = "错误";
                info.Tip = "<h5><p>添加行程失败</p></h5>";
                info.Url = "TripAdd.aspx";
            }

            context.Response.Write(JsonConvert.SerializeObject(info));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}